using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class CTPhieuXuatKhoDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveCTPhieuXuatKho(CTPhieuXuatKho ctphieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddCTPhieuXuatKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddCTPhieuXuatKho";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuatkho.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdPhieuXuatKho", DbType.Int32, ParameterDirection.Input, ctphieuxuatkho.IdPhieuXuatKho);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenHang", DbType.String, ParameterDirection.Input, ctphieuxuatkho.TenHang);
			cmd.Parameters.Add(param);
            param = CreateParameter("@MaHang", DbType.String, ParameterDirection.Input, ctphieuxuatkho.MaHang);
            cmd.Parameters.Add(param);
            param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, ctphieuxuatkho.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, ctphieuxuatkho.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, ctphieuxuatkho.DonGia);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThanhTien", DbType.Int64, ParameterDirection.Input, ctphieuxuatkho.ThanhTien);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, ctphieuxuatkho.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all CTPhieuXuatKho in DB
		public ResultObj GetAllCTPhieuXuatKho( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuXuatKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuXuatKho", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuXuatKho>();
			CTPhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatKho = Convert.ToInt32(reader["IdPhieuXuatKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
            item.MaHang = Convert.ToString(reader["MaHang"]);
            item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all CTPhieuXuatKho in DB with condition
		public ResultObj GetAllCTPhieuXuatKhoByID(CTPhieuXuatKho ctphieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<CTPhieuXuatKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllCTPhieuXuatKhoByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuatkho.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<CTPhieuXuatKho>();
			CTPhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new CTPhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdPhieuXuatKho = Convert.ToInt32(reader["IdPhieuXuatKho"]);
			item.TenHang = Convert.ToString(reader["TenHang"]);
            item.MaHang = Convert.ToString(reader["MaHang"]);
            item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ThanhTien = Convert.ToInt64(reader["ThanhTien"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a CTPhieuXuatKho out of DB with condition
		public ResultObj DeleteCTPhieuXuatKho(CTPhieuXuatKho ctphieuxuatkho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteCTPhieuXuatKho", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteCTPhieuXuatKho";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, ctphieuxuatkho.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of PhieuXuatKho from DB with condition
		public ResultObj GetPhieuXuatKho(int IdPhieuXuatKho)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<PhieuXuatKho> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetPhieuXuatKho", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdPhieuXuatKho);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<PhieuXuatKho>();
			PhieuXuatKho item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new PhieuXuatKho();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.NguoiLapPhieu = Convert.ToString(reader["NguoiLapPhieu"]);
			item.BoPhan = Convert.ToString(reader["BoPhan"]);
			item.NguoiNhan = Convert.ToString(reader["NguoiNhan"]);
			item.IdChiNhanhCongTy = Convert.ToInt32(reader["IdChiNhanhCongTy"]);
			item.LyDo = Convert.ToString(reader["LyDo"]);
			item.ChungTuKemTheo = Convert.ToString(reader["ChungTuKemTheo"]);
			item.IsDinhKhoan = Convert.ToInt32(reader["IsDinhKhoan"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
